package com.qf.simpleTriggerTest;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 2019/12/6
 * Administrator
 * springboot1206
 * 面向对象面向君  不负代码不负卿
 */
@Component
public class SimpleTriggerUtil {

    @Autowired
    @Qualifier("getsch")
    private Scheduler scheduler;

    //实现任务绑定
    @PostConstruct
    public void beginSimple(){
        try {
            //1.创建任务JobDetail
            // JobBuilder.newJob(任务类.class).withIdentity(自定义的任务形成)
            JobDetail jobDetail =
                    JobBuilder.newJob(MySimpleJob.class).withIdentity("jobaa").build();
            //2.创建触发器  withIntervalInSeconds(间隔时间)
            SimpleScheduleBuilder builder =
                    SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();
            SimpleTrigger simpleTrigger =
                    TriggerBuilder.newTrigger().withIdentity("mytrigger")
                    .withSchedule(builder).build();
            //3.使用调度器绑定触发器和任务
            scheduler.scheduleJob(jobDetail,simpleTrigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
